package com.jz.kufangkuqu.kuqu.controller;


import com.jz.kufangkuqu.kuqu.bean.*;
import com.jz.kufangkuqu.kuqu.service.KuQuService;
import com.jz.util.ResultBuildVO;
import com.jz.util.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


@RestController
@CrossOrigin
public class KuQuController {

    @Autowired
    private KuQuService kuQuService;

    //分页
    @PostMapping("storageSection/queryPage")
    public ResultVO querypage(@RequestBody KuQuSelect kuQuSelectVo){
        if(kuQuSelectVo.getPageIndex()==null){
            kuQuSelectVo.setPageIndex(1);
        }
        if(kuQuSelectVo.getPageSize()==null){
            kuQuSelectVo.setPageSize(10);
        }
        ResultVO resultVO = kuQuService.queryPage(kuQuSelectVo);
        return resultVO;
    }

    //新增
    @PostMapping("storageSection/add")
    public ResultVO add(@RequestBody @Valid KuQuAdd kuQuAddList, BindingResult bindingResult ){
        if (bindingResult.hasErrors()){
            return ResultBuildVO.error(bindingResult.getFieldError().getDefaultMessage());
        }
        ResultVO resultVO = kuQuService.add(kuQuAddList);
        return resultVO;
    }

    //修改
    @PostMapping("storageSection/update")
    public ResultVO update(@RequestBody @Valid KuQuUpdate kuQuUpdate, BindingResult bindingResult ){
        if (bindingResult.hasErrors()){
            return ResultBuildVO.error(bindingResult.getFieldError().getDefaultMessage());
        }
        ResultVO resultVO = kuQuService.update(kuQuUpdate);
        return resultVO;
    }
    //修改
    @GetMapping("storageSection/queryStorageSectionById")
    public ResultVO updateHuiXian( KuQuUpdateHuiXian kuQuUpdate ){
        ResultVO resultVO = kuQuService.updateHuiXian(kuQuUpdate);
        return resultVO;
    }
    //启用禁用
    @PostMapping("storageSection/updateStatus")
    public ResultVO updateStatus(@RequestBody @Valid KuQuUpdatestatus kuQuUpdatestatus, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuildVO.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return kuQuService.updateStatus(kuQuUpdatestatus);
    }
}